﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dack_Web2.BUS;
using Dack_Web2.DTO;
namespace Dack_Web2
{
    public partial class TaiKhoan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                drThang.Items.Clear();
                for (int i = 1; i < 13; i++)
                {
                    drThang.Items.Add(i.ToString());
                }
                drThang.SelectedIndex = 1;
                drNam.Items.Clear();
                for (int i = 1900; i < 2800; i++)
                {
                    drNam.Items.Add(i.ToString());
                }

            }
            if (!CurrentContext.IsLogged)
            {
                string url = ViewState["RefUrl"] == null ? "~/Default.aspx" :
                ViewState["RefUrl"].ToString();
                Response.Redirect(url);
                return;
            }
            else
            {
                if (!IsPostBack)
                {
                    KhachHangDTO NS = KhachHangBUS.GetKhachHang(CurrentContext.UserName);
                    if (NS != null)
                    {
                        txtDiaChi.Text = NS.DiaChi;
                        txtSDT.Text = NS.SoDienThoai;
                        txtTen.Text = NS.TenKhachHang;
                        txtTenDangNhap.Text = NS.TenDangNhap;
                        if (NS.GioiTinh)
                        {
                            rbNam.Checked = true;
                        }
                        else
                        {
                            rbNu.Checked = false;
                        }
                        txtDiaChi.Text = NS.DiaChi;
                        drThang.SelectedIndex = NS.NgaySinh.Month-1;
                        drThang_SelectedIndexChanged(null, null);
                        drNgay.SelectedIndex = NS.NgaySinh.Day-1;                       
                        drNam.Text = NS.NgaySinh.Year.ToString();
                    }
                    else
                    {
                        CongTyDTO CongTy = CongTyBUS.GetCongTy(CurrentContext.UserName);
                        chkCongTy.Checked = true;
                        chkCongTy_CheckedChanged(null, null);
                        txtDiaChi.Text = CongTy.DiaChi;
                        txtSDT.Text = CongTy.SoDienThoai;
                        txtTen.Text = CongTy.TenCongTy;
                        txtTenDangNhap.Text = CongTy.TenDangNhap;
                    }
                }
            }
           
        }

        protected void drThang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drThang.SelectedItem.Text == "2")
            {
                drNgay.Items.Clear();
                for (int i = 1; i < 30; i++)
                {
                    drNgay.Items.Add(i.ToString());
                }
            }
            else
            {
                drNgay.Items.Clear();
                for (int i = 1; i < 32; i++)
                {
                    drNgay.Items.Add(i.ToString());
                }
            }
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            Panel1.Enabled = !Panel1.Enabled;
            Panel1.Visible = Panel1.Enabled;
        }

        protected void chkCongTy_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCongTy.Checked == true)
            {
                lbTen.Text = "Tên công ty :";
                PanelNgaySinh.Visible = false;
            }
            else
            {
                lbTen.Text = "Tên khách hàng";
                PanelNgaySinh.Visible = true;
            }
        }


        protected void btnThayDoi_Click(object sender, EventArgs e)
        {
            if (CurrentContext.IsLogged)
            {
                User A = new User(CurrentContext.UserName, txtMatKhauCu.Text);
                if (chkDoiMatKhau.Checked)
                {
                    UserDTO Usn = new UserDTO();
                    Usn.MatKhau = txtMatKhauMoi.Text;
                    A = Dack_Web2.User.FromId(CurrentContext.UserName);
                    Usn.MaUser = A.MaUser;
                    Usn.TenDangNhap = CurrentContext.UserName;
                    UserBUS.Update(Usn);
                }
                if (!chkCongTy.Checked)
                {
                    KhachHangDTO KH = new KhachHangDTO();
                    KH.DiaChi = txtDiaChi.Text;
                    KH.GioiTinh = true;
                    if (rbNu.Checked)
                    {
                        KH.GioiTinh = false;
                    }
                    KH.SoDienThoai = txtSDT.Text;
                    KH.TenDangNhap = CurrentContext.UserName;
                    KH.TenKhachHang = txtTen.Text;
                    DateTime NS = new DateTime(int.Parse(drNam.SelectedItem.ToString()),
                                            int.Parse(drThang.SelectedItem.ToString()),
                                            int.Parse(drNgay.SelectedItem.ToString()));
                    KH.NgaySinh = NS;
                    KhachHangBUS KHBUS = new KhachHangBUS();
                    KHBUS.Update(KH);
                }
                else
                {
                    CongTyDTO CongTy = new CongTyDTO();
                    CongTy.DiaChi = txtDiaChi.Text;
                    CongTy.SoDienThoai = txtSDT.Text;
                    CongTy.TenCongTy = txtTen.Text;
                    CongTy.TenDangNhap = CurrentContext.UserName;
                    CongTyBUS.Update(CongTy);
                }
            }

        }

        protected void txtMatKhauCu_TextChanged(object sender, EventArgs e)
        {
            Dack_Web2.User KT = new User(CurrentContext.UserName, txtMatKhauCu.Text);
            if (KT.IsValidLogin())
            {
                lbWPass.Text = "Sai mật khẩu cũ ";
            }
            else
            {
                lbWPass.Text = "Đúng !!!";
            }
        }


    }
}